#include "leaveapplydialog.h"
#include "ui_leaveapplydialog.h"
#include <QSqlQuery>
#include <QSqlError>
#include <QMessageBox>
#include <QDebug>
#include "loginwidget.h"

LeaveApplyDialog::LeaveApplyDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::LeaveApplyDialog)
{
    ui->setupUi(this);
    initWidget();
}

LeaveApplyDialog::~LeaveApplyDialog()
{
    delete ui;
}

/**
 * @brief 初始化窗体
 */
void LeaveApplyDialog::initWidget()
{
    this->setWindowTitle("请假申请");
    ui->edit_startdate->setMinimumDateTime(QDateTime::currentDateTime());
    ui->edit_enddate->setMinimumDateTime(QDateTime::currentDateTime());
}

/**
 * @brief 请假申请提交
 */
void LeaveApplyDialog::on_btn_commit_clicked()
{
    QString reason = ui->edit_reason->toPlainText();
    QString startdate = ui->edit_startdate->text();
    QString enddate = ui->edit_enddate->text();

    QSqlQuery query;
    query.prepare("INSERT INTO leave_record (leave_username, reason, start_date, end_date, is_back) VALUES(:leave_username, :reason, :start_date, :end_date, :is_back)");
    query.bindValue(":leave_username", LoginWidget::userName);
    qDebug() << LoginWidget::userName;
    query.bindValue(":reason", reason);
    qDebug() << reason;
    query.bindValue(":start_date", startdate);
    qDebug() << startdate;
    query.bindValue(":end_date", enddate);
    qDebug() << enddate;
    query.bindValue(":is_back", 0);
    if (!query.exec())
    {
        qDebug()<<query.lastError();
    }
    else
    {
        QMessageBox::information(this, tr("提示"), tr("提交成功"), QMessageBox::Ok);
    }
    emit closeAndFlush();
}
